package com.cdream.basic.algorithm.leetcode.array;

/**
 * @author cdream
 * @date 2022/3/13
 */
public class Question393 {
    public static void main(String[] args) {
        new Question393().validUtf8(new int[]{197,130,1});
    }
    public boolean validUtf8(int[] data) {
        int n = data.length;

        for(int i = 0; i<n; ){
            int j = 7;
            while(j>0 && (((data[i]>>j)&1)==1)){
                j--;
            }
            int cnt = 7 -j;
            if(cnt>4 || cnt==1){
                return false;
            }
            for(int k = 1;k<cnt;k++){
                if(i+k>n){
                    return false;
                }
                int jk = data[i+k];
                if(jk>>7==1 && jk>>6==0){
                    continue;
                }else{
                    return false;
                }
            }
            if(cnt==0){
                i++;
            }else {
                i += cnt;
            }
        }
        return true;
    }
}
